import * as actionType from './constant'
import {getCurrentSong,getLyric} from 'services/player'
import {parseLyric} from 'utils/parse-lyric'

const changeCurrentSongAction = currentSong =>({
  type : actionType.CHANGE_CURRENT_SONG,
  currentSong
})

const changeCurrentIndexAction = currentSongIndex =>({
  type : actionType.CHANGE_CURRENT_INDEX,
  currentSongIndex
})

const ChangePlayListAction = playList =>({
  type : actionType.CHANGE_PLAY_LIST,
  playList
})

const getLyricAction = (lyric)=>({
  type : actionType.CHANGE_SONG_LYRIC,
  lyric
})

export const changeCurrentLyricIndex = (currentLyricIndex)=>({
  type : actionType.CHANGE_CURRENT_LYRIC_INDEX,
  currentLyricIndex
})

export const changeIsShowPannel = boolean =>({
  type : actionType.CHANGE_IS_SHOW_PANNEL,
  isShowPannel : boolean
})

export const changeIsShowMessage = boolean => ({
    type : actionType.CHANGE_IS_SHOW_Message,
    isShowMessage : boolean
})




export const ChangeCycleIndexAction = currentCycleIndex => {
  if(currentCycleIndex === 3){
    currentCycleIndex = 0;
  }
  return{
    type : actionType.CHANGE_CYCLE_INDEX,
    cycleIndex : currentCycleIndex
  }
}

export const ChangeSong = (tag)=>{
  return (dispatch,getState)=>{
    const playList = getState().getIn(["player","playList"]);
    const currentSongIndex = getState().getIn(["player","currentSongIndex"]);
    const cycleIndex = getState().getIn(["player","cycleIndex"]);
    let index = currentSongIndex;

    if(cycleIndex === 1){
      index = Math.floor(Math.random() * playList.length);
    }else{
      index += tag;
      if(index === playList.length){
        index = 0;
      }else if(index === -1){
        index = playList.length - 1;
      }
    }
    dispatch(changeCurrentIndexAction(index));
    dispatch(changeCurrentSongAction(playList[index]));
    getLyric(playList[index].id).then(res=>{
      const lyric = parseLyric(res.lrc.lyric);
      dispatch(getLyricAction(lyric))
    })
  }
}

export const getCurrentSongAction = (ids) =>{
  return (dispatch,getState) =>{
    const playList = getState().getIn(["player","playList"])
    const index  = playList.findIndex(item => item.id === ids)
    //找到了
    if(index !== -1){
      dispatch(changeCurrentIndexAction(index));
      dispatch(changeCurrentSongAction(playList[index]));
    }else{
      getCurrentSong(ids).then(res=>{
        const song = res.songs[0];
        const newPlayList = [...playList,song];
        dispatch(ChangePlayListAction(newPlayList));
        dispatch(changeCurrentIndexAction(newPlayList.length - 1));
        dispatch(changeCurrentSongAction(song));
      })
    }
    getLyric(ids).then(res=>{
      const lyric = parseLyric(res.lrc.lyric);
      dispatch(getLyricAction(lyric))
    })
    
  }
}

export const clearPlayListAction = ()=>{
  return dispatch => {
    dispatch(ChangePlayListAction([]));
  }
}

export const addPlayListSong = id =>{
  return (dispatch,getState) => {
    const playList = getState().getIn(["player","playList"]);
    const index = playList.findIndex(item => item.id === id);
    dispatch(changeIsShowMessage(true));
    if(index === -1){
      getCurrentSong(id).then(res=>{
        const song = res.songs[0];
        const newPlayList = [...playList,song];
        dispatch(ChangePlayListAction(newPlayList));
      })
    }
  }
}
